package com.kailong.controller;

import com.kailong.annotation.NoLogin;
import com.kailong.dto.PageResultDTO;
import com.kailong.dto.UserSearchDTO;


import com.kailong.entity.sys.UserEntity;
import com.kailong.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * created by kailong on 2025/8/4
 */
@RequestMapping("/v1/user")
@RestController
@Tag(name = "用户管理", description = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 根据id查询用户
     * @param id
     * @return
     */

    @PreAuthorize("hasAnyRole('USER')")
    @Operation(summary = "通过id查询用户信息", description = "通过id查询用户信息")
    @GetMapping("/findById")
    public UserEntity findById(Long id){
        return userService.fingById(id);
    }
    /**
     * 新增用户
     * @param userEntity
     * @return
     */
    @NoLogin
    @PostMapping("/insert")
    @Operation(summary = "新增用户", description = "新增用户信息")
    public void insert(@RequestBody UserEntity userEntity) {
       userService.insert(userEntity);
    }
    /**
     * 修改用户
     * @param userEntity
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改用户", description = "修改用户信息")
    public int update(@RequestBody UserEntity userEntity) {
        return userService.update(userEntity);
    }
    @PostMapping("/deleteById")
    @Operation(summary = "删除用户", description = "根据ID删除用户")
    public int delete( @RequestParam  Long id) {
        return userService.delete(id);
    }
    @PostMapping("/search")
    @Operation(summary = "搜索用户", description = "根据条件搜索用户")
    public PageResultDTO<UserEntity> search(@RequestBody UserSearchDTO searchDTO) {
        PageResultDTO<UserEntity> result = userService.searchUsers(searchDTO);
        return result;
    }

}

